package com.concurrent.threads.foo;

/**
 * @ClassName Foo2
 * @Description TODO
 * @Author li
 * Date 2021/8/23 0:35
 * Copyright
 **/
class Foo2 {
    private final Object lock = new Object();
    private boolean first;
    private boolean second;
    public Foo2() {

    }

    public void first(Runnable printFirst) throws InterruptedException {
        synchronized (lock){
            // printFirst.run() outputs "first". Do not change or remove this line.
            printFirst.run();
            first = true;
            lock.notifyAll();
        }

    }

    public void second(Runnable printSecond) throws InterruptedException {
        synchronized (lock){
            if (!first){
                lock.wait();
            }
            // printSecond.run() outputs "second". Do not change or remove this line.
            printSecond.run();
            second = true;
            lock.notifyAll();
        }

    }

    public void third(Runnable printThird) throws InterruptedException {
        synchronized (lock){
            if (!second){
                lock.wait();

            }
            printThird.run();
        }
        // printThird.run() outputs "third". Do not change or remove this line.

    }
}